package com.study.servlet;

import com.study.dao.CustomerDao;
import com.study.dao.impl.CustomerDaoImpl;
import com.study.model.Customer;
import com.study.util.GsonHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author WS
 */
@WebServlet(name = "loginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends BaseServlet {
    private CustomerDao dao=new CustomerDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req,resp);
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        Customer customer=dao.login(username,password);
        PrintWriter out = resp.getWriter();
        String json;
        if (customer==null){
            json= GsonHelper.toJsonWithDate("登录失败\n\t请检查用户名密码");
        }
        else {
            req.getSession().setAttribute("CURRENTCUS",customer);
            json= GsonHelper.toJsonWithDate("登录成功");
        }
        out.println(json);
        out.flush();
        out.close();
    }
}
